// -*- c++ -*-
//
// deliciousapi.h
//
// Copyright (C) 2005 Petter Stokke <ummo@hellokitty.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the
// Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
//

#ifndef DELICIOUSAPI_H
#define DELICIOUSAPI_H

#include "rss.h"
#include "bookmarkapi.h"

#include <qobject.h>
#include <qvaluelist.h>
#include <qintdict.h>
#include <qstring.h>
#include <qptrlist.h>
#include <qdom.h>

#include <kurl.h>
#include <kio/job.h>

class QTimer;

class DeliciousAPIJob;
class DeliciousRenameTagJob;
class DeliciousGetPostsJob;

class DeliciousAPI : public BookmarkAPI
{
    Q_OBJECT
public:
    DeliciousAPI( QObject* parent = 0, const char* name = "DeliciousAPI", const KURL& apiURL = KURL() );

    const KURL& apiURL() const;

    RenameTagJob* renameTag( const QString& tag, const QString& newTag );
    GetPostsJob* getPosts( const QDateTime& lastUpdate );

private slots:
    void scheduler();
    void slotJobDone();

private:
    void scheduleJob( DeliciousAPIJob* job );
    void findUser();

    KURL m_apiURL;
    QPtrList<DeliciousAPIJob> m_jobs;
    QTimer *m_timer;
    QString m_user;
};

class DeliciousAPIJob : public BookmarkAPIJob
{
    Q_OBJECT
public:
    DeliciousAPIJob( DeliciousAPI* api, const char* name = "DeliciousAPIJob" );

    DeliciousAPI* api() const;
    const KURL& apiURL() const;

    virtual void start() = 0;

    bool isDone() const;
    bool isRunning() const;

protected slots:
    void jobResult( KIO::Job* job );
    void slotTotalSize( KIO::Job* job, KIO::filesize_t size );
    void slotProcessedSize( KIO::Job* job, KIO::filesize_t size );

protected:
    void done();
    void startJob( const KURL& url );
    virtual void processData() = 0;

    DeliciousAPI* m_api;
    QByteArray m_data;
    QDomDocument m_xml;
    bool m_isDone, m_isRunning;
    KURL m_commandURL;
    KIO::filesize_t m_processedSize, m_totalSize;

};

class RSSJob : public DeliciousAPIJob
{
    Q_OBJECT
public:
    RSSJob( DeliciousAPI* api, const char* name );

protected:
    void processData();
    virtual void processFeed( const RSSFeed& feed ) = 0;
};

class DeliciousRenameTagJob : public DeliciousAPIJob, public RenameTagJob
{
    Q_OBJECT
public:
    DeliciousRenameTagJob( DeliciousAPI* api, const QString& oldTag, const QString& newTag );

    virtual QString oldTag() const;
    virtual QString newTag() const;

    void start();

protected:
    void processData();

private:
    QString m_oldTag, m_newTag;
};

class DeliciousGetPostsJob : public DeliciousAPIJob, public GetPostsJob
{
    Q_OBJECT
public:
    DeliciousGetPostsJob( DeliciousAPI* api, const QDateTime& lastUpdate );

    virtual const QPtrList<Post>& posts() const;
    virtual QDateTime updateTime() const;
    virtual bool noUpdateNeeded() const;

    void start();

protected:
    void processData();

private:
    QDateTime m_lastUpdate, m_updateTime;
    int m_state;
    QPtrList<Post> m_posts;
    bool m_noUpdateNeeded;
};

/*
class GetGlobalTagJob : public RSSJob
{
    Q_OBJECT
public:
    GetGlobalTagJob( DeliciousAPI* api, const QString& tag );

    void start();

signals:
    void gotGlobalTag( Collection* collection, const QString& tag );

protected:
    void processFeed( const RSSFeed& feed );

private:
    QString m_tag;
};
*/

#endif
